#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#define F_CPU 14745000UL			// CPU FREQ
#include <util/delay.h>

#include "USART.h"
#include "TWI.h"

#ifndef MAIN_H_
#define MAIN_H_

#define WAIT_MODE bit_is_set(PINB,0)

// PIN 0,1,2,3
#define D_LIGHT_ON(PIN)			PORTA |= (1<<PIN);
#define D_LIGHT_OFF(PIN)		PORTA &= ~(1<<PIN);

#define FALSE	0
#define TRUE	1


/* TWI COMANDS */
#define STYR_FORWARD			0x11
#define STYR_FOWARD_RIGHT		0x12
#define STYR_FOWARD_LEFT		0x13
#define STYR_BACK				0x14
#define STYR_STOP				0x15
#define	STYR_ROTATE_RIGHT		0x16
#define STYR_ROTATE_LEFT		0x17
#define STYR_NIBBER				0X18

/* TWI STATES */
#define TWI_DATA_START			0x00
#define TWI_DATA_STOP			0x01

#define TWI_STATE_IDLE			0x00	// Defualt satte
#define TWI_STATE_GSEN			0x01	// Get sensor values
#define TWI_STATE_GREG			0x02	// Get regulation values

/* USART STATES */
#define USART_DATA_START		0x01	// Start condition
#define USART_DATA_STOP			0x00	// stop condition
#define USART_DATA_CALIBRATION	0xFF	// Calibration value
#define USART_DATA_CALIBRATE	0xFE	// Calibration value
#define USART_DATA_REGULATION	0x00


#define USART_STATE_IDLE		0x00	// Defualt state
#define USART_STATE_INCOMING	0x02	// Connected data incoming...
#define USART_STATE_MANAGE		0x03	// All connected bytes have been received, manage the data.

/* Functions */
void	sendtoTWI	(const int8_t, const int8_t );
int8_t	recieveTWI	(const int8_t);

/* Variables */
volatile uint8_t USART;
volatile uint8_t TWI;

volatile uint8_t usartByteCounter;
volatile uint8_t timerCounter;

/* FIFO */
	/* Settings */
	#define ARRAY_SIZE 255

	/* Functions */
	void enqueue(const int8_t);
	uint8_t dequeue();
	uint8_t peek();

	/* Variables */
	volatile uint8_t usartData[ARRAY_SIZE];
	volatile uint8_t usartDataEnqueuePosition;
	volatile uint8_t usartDataDequeuePosition;
	
	volatile uint8_t usartDataEnqueuePositionRoundCounter;
	volatile uint8_t usartDataDequeuePositionRoundCounter;

#endif